﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using studentPortal.Models;

namespace studentPortal.Controllers
{
    public class GroupController : Controller
    {
        //
        // GET: /Group/
        IAccount ac;

        public GroupController(IAccount ac1)
        {
            ac = ac1;
        }

        public ActionResult Index()
        {

            return View();
        }
        public ActionResult Group(String id)
        {
            Database1Entities1 cx = new Database1Entities1();
            int id1 = int.Parse(id);
            group q = cx.groups.First(x=>x.groupId == id1);
            List<group> g = cx.groups.ToList();
            List<post> p = cx.posts.Where(y => y.groupId == id1).ToList();
            

            ViewBag.x = p;
            ViewBag.z = g;
            return View(q);
        }
        public ActionResult CreateGroup()
        {

            return View();
        }
        public ActionResult SaveGroup(group obj)
        {
            Database1Entities1 cx = new Database1Entities1();
            String owner = Session["username"].ToString();
            
            group g = new group();
            g.owner = owner;
            g.noOfPost = 0;
            g.groupName = obj.groupName;
            g.desc = obj.desc;
            g.creationTime = DateTime.Now.ToString();
            cx.groups.Add(g);
            cx.SaveChanges();

            int id = cx.groups.First(x=>x.groupName.Equals(obj.groupName)).groupId;

            return Redirect("../Group/Group?id="+id);
        }
    }
}
